<?php
class UM_Cat_Widgets extends WP_Widget {

   public function __construct(){
       parent::__construct(
            'um_catsidebar_widget',
            'UM Categories',
            array('description' => 'A UM Customize Widget ' ));
    
   }
   
   public function widget($args, $instance)
    {
         extract( $args );
        if ( isset($instance['title'])):
            $title = apply_filters( 'widget_title', $instance['title'] );
        else:
            $title = '';
        endif;
        
        $args = array(
        'type'                     => 'post',
        'child_of'                 => 0,
        'parent'                   => '',
        'orderby'                  => 'name',
        'order'                    => 'ASC',
        'hide_empty'               => 1,
        'hierarchical'             => 1,
        'exclude'                  => '',
        'include'                  => '',
        'number'                   => '5',
        'taxonomy'                 => 'category',
        'pad_counts'               => false );
        
        $categories = get_categories($args);
        
        //print_r($categories);
        
       ?>
    <section id="categories">
        <?php if ( $title ): 
            echo $title;
        ?>
        <?php else: ?>
            <h3>Categories</h3>
            <ul>
                <?php foreach($categories as $category):?>
                <li>
                    <a href="<?php echo  get_category_link( $category->term_id ); ?>" class="group"> 
                        <span><?php echo $category->name; ?></span>
                        <span class="post-count"> <?php echo $category->count; ?> </span>
                    </a>
                </li>
                <?php endforeach; ?>
            </ul>
        <?php endif; ?>
    </section>
       <?php
    }
    
    	public function update( $new_instance, $old_instance ) {
		// processes widget options to be saved
      $instance = array();
      $instance['title'] = strip_tags($new_instance['title']);
      
      return $instance;
	}
}
add_action( 'widgets_init', create_function( '', 'register_widget( "UM_Cat_Widgets" );' ) );

class UM_Posttype_Widgets extends WP_Widget {

   public function __construct(){
       parent::__construct(
            'um_posttype_widget',
            'UM Post Type',
            array('description' => 'A UM Customize Widget ' ));
    
   }
   
   public function widget($args, $instance)
    {
     
       ?>
    <section id="categories">
		<?php
		if ( isset($instance['title'])):
					$title = apply_filters( 'widget_title', $instance['title'] );
			else:
					$title = '';
			endif;
				
		$args = array('_builtin' => false);
		$post_types = get_post_types($args ); //print_r($post_types); 
		unset($post_types['img_banner']);
		?> 
    
        <?php if ( $title ): 
            echo $title;
        ?>
        <?php else: ?>
            <h3>Categories</h3>
				<?php endif; ?>
            <ul>
                <?php foreach($post_types as $key => $value):?>
								<?php $post_type = get_post_type_object( $value ); 
								$count = wp_count_posts( $value, false );
								if( $post_type )
								{
									
								?>
                <li>
                    <a href="<?php echo get_post_type_archive_link( $value ); ?>" class="group"> 
                        <span><?php echo $post_type->labels->name; ?></span>
                        <span class="post-count"> <?php echo $count->publish; ?> </span>
                    </a>
                </li>
								<?php
								}
								?>
                <?php endforeach; ?>
            </ul>
       
    </section>
       <?php
    }
    
    	public function update( $new_instance, $old_instance ) {
		// processes widget options to be saved
      $instance = array();
			
      $instance['title'] =isset($new_instance['title']) ? strip_tags($new_instance['title']) : '';
      
      return $instance;
	}
}
add_action( 'widgets_init', create_function( '', 'register_widget( "UM_Posttype_Widgets" );' ) );

class UM_Recent_Post_Widgets extends WP_Widget {

   public function __construct(){
       parent::__construct(
            'um_recentpostsidebar_widget',
            'UM Recent Posts',
            array('description' => 'A UM Customize Recent Post Widget ' ));
    
   }
   
   public function widget($args, $instance)
    {
         extract( $args );
        if ( isset($instance['title'])):
            $title = apply_filters( 'widget_title', $instance['title'] );
        else:
            $title = '';
        endif;
        
      $args = array('numberposts'     => 5,
                    'offset'          => get_option( 'posts_per_page'),
                    'category'        => '',
                    'orderby'         => 'post_date',
                    'order'           => 'DESC',
                    'include'         => '',
                    'exclude'         => '',
                    'meta_key'        => '',
                    'meta_value'      => '',
                    'post_type'       => array('post','travel-activity','food','entertainment','activities'),
                    'post_mime_type'  => '',
                    'post_parent'     => '',
                    'post_status'     => 'publish',
                    'suppress_filters' => true);
      
      $posts = get_posts($args); 
        
       ?>
    <section id="recent-posts-widget" class="posts-list-widget">
        <?php if ( $title ): 
            echo $title;
        ?>
        <?php else: ?>
            <h3>Recent Posts </h3>
           
                <?php foreach($posts as $post):?>
               <article class="group">
                   <a href="<?php echo get_permalink($post->ID); ?>"> <?php 
                   if (get_the_post_thumbnail($post->ID,'thumbnail')):
                   echo get_the_post_thumbnail($post->ID,'thumbnail') ?></a>
                  <?php  else: ?>
                   <img src="<?php echo IMG , 'article.png'; ?>" alt="#" /> 
                <?php
                   endif;
                ?>
                    <div class="text">
                        <a href="<?php echo get_permalink($post->ID); ?>"><h4 class="entry-title"><?php echo $post->post_title; ?></h4></a>
                        <p class="entry-desc"><?php echo wp_trim_words( $post->post_excerpt, 10 ); ?></p>
                    </div>
                </article>
                <?php endforeach; ?>
            
        <?php endif; ?>
    </section>
       <?php
    }
    
    	public function update( $new_instance, $old_instance ) {
		// processes widget options to be saved
      $instance = array();
      $instance['title'] = strip_tags($new_instance['title']);
      
      return $instance;
	}
}

add_action( 'widgets_init', create_function( '', 'register_widget( "UM_Recent_Post_Widgets" );' ) );

